var payUtil = require("wxPayUtil.js");
function wxpay(app, money, orderId, redirectUrl) {
  var remark = "在线充值";
  var nextAction = {};
  if (orderId != 0) {
    remark = "支付订单 ：" + orderId;
    nextAction = { type: 0, id: orderId };
  }
  wx.request({
    url: 'https://api.it120.cc/' + app.globalData.subDomain + '/pay/wxapp/get-pay-data',
    data: {
      token: wx.getStorageSync('token'),
      money:money,
      remark: remark,
      payName:"在线支付",
      nextAction: nextAction
    },
    //method:'POST',
    success: function(res){
      if(res.data.code == 0){
        // 发起支付
        wx.requestPayment({
          timeStamp:res.data.data.timeStamp,
          nonceStr:res.data.data.nonceStr,
          package:'prepay_id=' + res.data.data.prepayId,
          signType:'MD5',
          paySign:res.data.data.sign,
          fail:function (aaa) {
            wx.showToast({title: '支付失败:' + aaa})
          },
          success:function () {
            wx.showToast({title: '支付成功', icon: 'success',
              duration: 2000})
            wx.redirectTo({
              url: redirectUrl
            });
          }
        })
      } else {
        wx.showToast({ title: '服务器忙' + res.data.code + res.data.msg})
      }
    }
  })
}
//充值支付
function getPayU(app,money,reactUrl) {
  var rechargeParamJson = {
    memberId: app.globalData.loginUserInfo.userInfo.id,
    openId: app.globalData.loginUserInfo.userInfo.openId,
    rechargeAmount: money
  }
  //支付
  wx.request({
    url: app.globalData.baseUrl + '/member/recharge/rechargePayment',
    data: JSON.stringify(rechargeParamJson),
    method: 'POST',
    success: function (res) {
      //调用成功
      var prepayId = res.data.data.prepayId;
      var appId = res.data.data.appId;
      var key = res.data.data.mchApiSecretKey;
      var timeStamp = payUtil.getTimeStamp();
      var nonceStr = payUtil.getNonceStr();
      var packages = "prepay_id=" + prepayId;
      var signType = payUtil.getMD5SignType();
      var paySign = payUtil.getSign(appId, nonceStr, timeStamp, prepayId, signType, key);
      wx.requestPayment({
        timeStamp: timeStamp,
        nonceStr: nonceStr,
        package: packages,
        signType: signType,
        paySign: paySign,
        success: function (res) {//支付完成
          if(res.errMsg=="requestPayment:ok") {
            wx.showToast({title: '支付成功',icon: 'success',
                duration: 2000})
            if (reactUrl == 1) {
              wx.navigateBack({
                delta: reactUrl
              })
            } else {
              wx.redirectTo({
                url: reactUrl
              });
            }
          }else{
            wx.showToast({title: '支付失败', icon: 'none',
              duration: 2000})
          }
        },
        fail: function (res) {//支付失败
          wx.showToast({ title: '支付失败',icon: 'none',
            duration: 2000})
        }
      })
    },
    fail: function (res) {
      //调用失败
      if(res.errMsg=="requestPayment:fail cancel") {
        wx.showToast({title: "支付失败",icon: 'none',
          duration: 2000})
      }
    }
  })
}
//微信支付
function wechatPay(reactUrl,prepayId,appId,key) {
      //调用成功
      var prepayId = prepayId;
      var appId = appId;
      var key = key;
      var timeStamp = payUtil.getTimeStamp();
      var nonceStr = payUtil.getNonceStr();
      var packages = "prepay_id=" + prepayId;
      var signType = payUtil.getMD5SignType();
      var paySign = payUtil.getSign(appId, nonceStr, timeStamp, prepayId, signType, key);
      wx.requestPayment({
        timeStamp: timeStamp,
        nonceStr: nonceStr,
        package: packages,
        signType: signType,
        paySign: paySign,
        success: function (res) {//支付完成
          if(res.errMsg=="requestPayment:ok") {
            wx.showToast({title: '支付成功'})
            if (reactUrl == 1) {
              wx.navigateBack({
                delta: reactUrl
              })
            } else {
              wx.redirectTo({
                url: reactUrl
              });
            }
          }else{
            wx.showToast({title: '支付失败',icon: 'none',
              duration: 2000})
          }
        },
        fail: function (res) {//支付失败
          if(res.errMsg=="requestPayment:fail cancel") {
            wx.showToast({title: "支付失败",icon: 'none',
              duration: 2000})
          }
        }
      })
}

module.exports = {
  wxpay: wxpay,
  wxwechatpay: getPayU,
  wxpay:wechatPay
}
